﻿using ConfExpert.Forms.Base;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Sistem
{
    public partial class UretimSql : XForm
    {
        public UretimSql()
        {
            InitializeComponent();
        }

        private void UretimSql_Load(object sender, EventArgs e)
        {
            gridView1.Init("GetUretimSql", 0);
            gridView1.MakeColumnInvisible("UretimSqlId");
            gridView1.FormatAsNumber(0, "LineCount");
            gridView1.SetCaptions("1:Model", "2:Artikel", "3:Renk", "4:OrderNr");
            gridView1.MakeReadOnly("1", "2", "3", "4");
            gridView2.MakeReadOnly();
            gridView2.DoubleClick += gridView2_DoubleClick;

            gridView1.FocusedRowChanged += gridView1_FocusedRowChanged;
            btnKaydet.ItemClick += btnKaydet_ItemClick;
            //splitContainerControl1.AutoSize();

            gridView1.RowCellStyle += gridView1_RowCellStyle;
            gridView1.AddSummary("LineCount");
            gridView1.BestFitColumns();
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.Update("", "UretimSql");
            Notify("Kaydedildi.");
        }

        void gridView2_DoubleClick(object sender, EventArgs e)
        {
            int colInt = gridView1.FocusedColumn.FieldName.ToInt();
            if (colInt > 0)
            {
                gridView1.SetFocusedRowCellValue(colInt.ToStr(), gridView2.FocusedColumn.FieldName);
                if (colInt < 4)
                    gridView1.FocusedColumn = gridView1.Columns[(colInt + 1).ToStr()];
                gridView1.BestFitColumns();
            }
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            btnKaydet.PerformClick();

            if (gridView1.GetFocusedDataRow() != null)
            {
                int CustomerId = gridView1.GetFocusedDataRow()["CustomerId"].ToInt();
                d = ExecuteDataSet("GetUretimSql", CustomerId);

                gridView2.BeginUpdate();
                gridView2.Init(d.Tables[0]);
                InitFromDB(gridView2, CustomerId);
                gridView2.EndUpdate();

                gridView2.BestFitColumns();
                gridView2.MakeReadOnly();
            }
        }

        void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
                if (e.Column.FieldName == "CustomerId")
                    //if (gridView1.GetRowCellValue(e.RowHandle, "MyCust").ToInt() > 0)
                        e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
        }


        DataSet d;
        #region Gridin caption ve sırasını setleme
        private void InitFromDB(GridView gridView, int Parameter)
        {
            string Culture = "TR";
            DataTable dt = d.Tables[1];

            gridView2.MakeColumnInvisible("Scan",
                "A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10",
                "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20",
                "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28");
            //foreach (GridColumn col in gridView.Columns)
            //    col.Visible = false;

            foreach (DataRow dr in dt.Rows)
            {
                string colName = dr["ColumnName"].ToString();

                if (gridView.Columns[colName] != null)
                {
                    if (dt.Columns.Contains("Visible") && dr["Visible"].ToInt() > 0)
                        if (dt.Columns.Contains("Position") && dr["Position"] != null)
                            gridView.Columns[colName].VisibleIndex = (int)dr["Position"];
                    if (dr["Caption" + Culture] != null)
                        if (gridView.Columns[colName].Caption != dr["Caption" + Culture].ToString())
                            gridView.Columns[colName].Caption = dr["Caption" + Culture].ToString();
                }
            }
            gridView.ShowOnlyNonblankColumns();
            gridView.MakeColumnInvisible("PRO");
        }
        #endregion

    }
}
